﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Net;
using thailandProject.Models;


public class GMailer
{
    //private static string password = "1t1swh@t1t1s";
    //private static string user = "thailandtester";
    private static string host = "smtp.gmail.com";
    private static int port = 587;
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="subject"></param>
    /// <param name="to"></param>
    /// <param name="body"></param>
    /// <param name="isBodyHtml"></param>
    /// <returns>true on success and false otherwise</returns>
    public static bool SendMail(string subject, string to, string body = null, bool isBodyHtml = true)
    {
        String username = "";
        String password = "";
        String gmailAddress = "";
        try
        {
            // Convert Message
            if (isBodyHtml && body != null)
            {
                body = htmlConverter.Convert(body);
            }

            // Get the Username and password
            ThailandEntities a = new ThailandEntities();
            int richardUserID = 1;
            var prefs2 = from pref in a.EMAILPREFERENCEs
                         where pref.userID == richardUserID
                         select pref;

            List<EMAILPREFERENCE> richardPref = prefs2.ToList();
            if (richardPref.Count != 0)
            {
                EMAILPREFERENCE richUserPref = richardPref[0];
                username = richUserPref.gmailUserName;
                password = richUserPref.gmailPassword;
                gmailAddress = username + "@gmail.com";
            }
            else
            {
                // We have an error, we need to report this one...
                Exception ex = new Exception("Richad Katze did not have any email preferences, this needs to be fixed!");
                ErrorLog.logError(ex, "This is Important!!! please fix this ASAP!");
            }

            //Mail Message
            MailMessage m = new MailMessage();
            m.From = new MailAddress(gmailAddress);
            m.To.Add(to);
            m.Subject = subject;
            m.IsBodyHtml = isBodyHtml;
            m.BodyEncoding = Encoding.UTF8;
            m.Body = body;
            m.Priority = MailPriority.Normal;

            // Smtp config
            SmtpClient c = new SmtpClient(host, port);
            c.EnableSsl = true;
            c.DeliveryMethod = SmtpDeliveryMethod.Network;
            c.UseDefaultCredentials = false;
            c.Credentials = new NetworkCredential(username, password);

            c.Send(m);
        }
        catch (Exception ex)
        {
            String additionalInformation = "username: " + username +
                                            " password: " + password +
                                            " gmailAddress: " + gmailAddress +
                                            " subject: " + subject +
                                            " to: " + to +
                                            " body: " + body;
            ErrorLog.logError(ex, additionalInformation);
            return false;
        }
        return true;
    }
}